//
//  AlbumDetailsCell.m
//  SuperRadio
//
//  Created by GYRu on 14-12-17.
//  Copyright (c) 2014年 三个臭皮匠. All rights reserved.
//

#import "AlbumDetailsCell.h"
#import "AlbumDatailsOfList.h"
#import "UIImageView+WebCache.h"
#import "AudioButton.h"

#import "AlbumDetailsCell.h"

@interface AlbumDetailsCell ()




// 专辑音频的标题
@property(nonatomic, retain) UILabel *albumVoiceTitle;
// 播主名
@property(nonatomic, retain) UILabel *albumVoiceUser;
// 专辑音频的来源
@property(nonatomic, retain) UILabel *albumVoiceSource;
// 专辑音频的播放次数小图标
@property(nonatomic, retain) UIImageView *albumVoicePlayCountsImage;
// 专辑音频的播放次数
@property(nonatomic, retain) UILabel *albumVoicePlayCounts;
// 专辑音频的播放时长小图标
@property(nonatomic, retain) UIImageView *albumVoiceLenImage;
// 专辑音频的播放时长
@property(nonatomic, retain) UILabel *albumVoiceLen;
// 专辑音频的海报
@property(nonatomic, retain) UIImageView *albumVoiceImage;
// 播放按钮
@property(nonatomic, retain) UIButton *albumVoicePlay;
// 下载小图标
@property(nonatomic, retain) UIButton *download;



@end

@implementation AlbumDetailsCell

#pragma mark 初始化属性
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.albumVoiceImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.albumVoiceImage];
        [_albumVoiceImage release];
        
        self.albumVoiceTitle = [[UILabel alloc] init];
        [self.contentView addSubview:self.albumVoiceTitle];
        // 使 albumVoiceTitle 不限制行数
        self.albumVoiceTitle.numberOfLines = 2;
        [_albumVoiceTitle release];
        
        self.albumVoiceUser = [[UILabel alloc] init];
        self.albumVoiceUser.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:self.albumVoiceUser];
        [_albumVoiceUser release];
        
        
        self.albumVoicePlayCounts = [[UILabel alloc] init];
        self.albumVoicePlayCounts.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.albumVoicePlayCounts];
        [_albumVoicePlayCounts release];
        
        self.albumVoicePlayCountsImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.albumVoicePlayCountsImage];
        [_albumVoicePlayCountsImage release];
        
        self.albumVoiceLen = [[UILabel alloc] init];
        self.albumVoiceLen.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:self.albumVoiceLen];
        [_albumVoiceLen release];
        
        self.albumVoiceLenImage = [[UIImageView alloc] init];
        [self.contentView addSubview:self.albumVoiceLenImage];
        [_albumVoiceLenImage release];
        
       // self.albumVoicePlay = [UIButton buttonWithType:UIButtonTypeSystem];
       // [self.contentView addSubview:self.albumVoicePlay];
        
        self.audioButton = [[AudioButton alloc] init];
        [self.contentView addSubview:self.audioButton];
        //[self.audioButton addTarget:self action:@selector(playAudio:) forControlEvents:UIControlEventTouchUpInside];
       // self.download = [UIButton buttonWithType:UIButtonTypeSystem];
       // [self.contentView addSubview:self.download];
        
        
        
    }
    
    return self;
}

//- (void)playAudio:(AudioButton *)button
//{
//    _audioPlayer =  [AudioPlayer shareInstance];
//    
//    if ([_audioPlayer.button isEqual: button]) {
//        
//        [_audioPlayer play];
//        
//    } else {
//        
//        [_audioPlayer stop];
//        
//        _audioPlayer.button = button;
//        
//        _audioPlayer.url = [NSURL URLWithString:self.albumDatailsOfList.playUrl32];
//        
//        [_audioPlayer play];
//    }
//    
//}


#pragma mark 重写setter方法 对类对象属性赋值
-(void)setAlbumDatailsOfList:(AlbumDatailsOfList *)albumDatailsOfList
{
    if (_albumDatailsOfList != albumDatailsOfList) {
        [_albumDatailsOfList release];
        _albumDatailsOfList = [albumDatailsOfList retain];
    }
    // 专辑音频的标题
    self.albumVoiceTitle.text = _albumDatailsOfList.title;
    // 专辑音频的播主名
    self.albumVoiceUser.text =[NSString stringWithFormat:@"by: %@", _albumDatailsOfList.nickname];
    // 专辑的海报
    [self.albumVoiceImage setImageWithURL:[NSURL URLWithString:[self.albumDatailsOfList.coverLarge stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
    // 播放次数
    
    if ([_albumDatailsOfList.playtimes floatValue] > 10000) {
        
        CGFloat PlayCounts = [_albumDatailsOfList.playtimes floatValue] / 10000;
        self.albumVoicePlayCounts.text = [NSString stringWithFormat:@"%0.2lf 万", PlayCounts];
    } else {
        self.albumVoicePlayCounts.text = [NSString stringWithFormat:@"%@", _albumDatailsOfList.playtimes];
    }
    
    
    // 播放时长
    // 判断分,秒是否小于10
    if ([self.albumDatailsOfList.duration intValue] % 60 < 10  && [self.albumDatailsOfList.duration intValue] / 60 < 10) {
        // 分和秒都小于10, 则分和秒前面都加0
        self.albumVoiceLen.text = [NSString stringWithFormat:@"0%d:0%d", [self.albumDatailsOfList.duration intValue] / 60, [self.albumDatailsOfList.duration intValue] % 60];
    } else if ([self.albumDatailsOfList.duration intValue] / 60 < 10) {
        // 分小于10, 则分前面加0
        self.albumVoiceLen.text = [NSString stringWithFormat:@"0%d:%d", [self.albumDatailsOfList.duration intValue] / 60, [self.albumDatailsOfList.duration intValue] % 60];
    } else if ([self.albumDatailsOfList.duration intValue] % 60 < 10) {
        // 秒小于10, 则秒前面加0
        self.albumVoiceLen.text = [NSString stringWithFormat:@"%d:0%d", [self.albumDatailsOfList.duration intValue] / 60, [self.albumDatailsOfList.duration intValue] % 60];
    } else {
        // 否则分和秒都不加0
        self.albumVoiceLen.text = [NSString stringWithFormat:@"%d:%d", [self.albumDatailsOfList.duration intValue] / 60, [self.albumDatailsOfList.duration intValue] % 60];
    }
    
    
    
    // 给固定imageView添加图片
    NSString *path = [[NSBundle mainBundle] pathForResource:@"bofang" ofType:@"png"];
    self.albumVoicePlayCountsImage.image = [UIImage imageWithContentsOfFile:path];
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"shijian" ofType:@"png"];
    self.albumVoiceLenImage.image = [UIImage imageWithContentsOfFile:path1];
    
    NSString *path2 = [[NSBundle mainBundle] pathForResource:@"bofanghei" ofType:@"png"];
    [self.albumVoicePlay setBackgroundImage:[UIImage imageWithContentsOfFile:path2] forState:UIControlStateNormal];
    
   // NSString *path3 = [[NSBundle mainBundle] pathForResource:@"xiazai" ofType:@"png"];
    //[self.download setBackgroundImage:[UIImage imageWithContentsOfFile:path3] forState:UIControlStateNormal];
}

#pragma mark 自定义cell的布局
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    NSInteger width = self.contentView.bounds.size.width / 6;
    NSInteger height = self.contentView.bounds.size.height / 3;
    
    self.audioButton.frame =CGRectMake(10, 30, height + 5, height + 5);
    
   // self.albumVoicePlay.frame = CGRectMake(15, 15, height + 5, height + 5);
    
    self.albumVoiceTitle.frame = CGRectMake(width, 5, width * 3 + 10, height + 10);
    
    self.albumVoiceUser.frame = CGRectMake(width, height +10, width * 3, height - 10);
    
    self.albumVoiceImage.frame = CGRectMake(width * 4 + 30, 10, width * 2 - 30, width * 2 - 30);
    
    self.albumVoicePlayCountsImage.frame = CGRectMake(width + 5, height * 2 + 8, height - 15, height - 15);
    self.albumVoicePlayCounts.frame = CGRectMake(width + width / 2 - 5, height * 2, width, height);
    
    self.albumVoiceLenImage.frame = CGRectMake(width * 3, height * 2 + 8, height - 15, height - 15);
    self.albumVoiceLen.frame = CGRectMake(width * 3 + width / 2 - 8, height * 2, width, height);
    
   // self.download.frame = CGRectMake(width * 5 + 10, height * 2 + 10, height - 15 , height - 15);
    // albumVoiceTitle自适应高度
    [self.albumVoiceTitle sizeToFit];
    
}


- (void)awakeFromNib {
    // Initialization code
    
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}



- (void)dealloc
{
    [_albumDatailsOfList release];
    
    [_albumVoiceTitle release];
    [_albumVoiceUser release];
    [_albumVoiceSource release];
    [_albumVoicePlayCounts release];
    [_albumVoicePlayCountsImage release];
    [_albumVoiceImage release];
    [_albumVoiceLen release];
    [_albumVoiceLenImage release];
    [_download release];
    
    
    [super dealloc];
}





@end
